{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2c73611",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import xlwings as xw\n",
    "import time\n",
    "\n",
    "def convert_xls_to_xlsx(input_folder, output_folder):\n",
    "    # 验证输入和输出文件夹路径是否有效\n",
    "    if not os.path.isdir(input_folder) or not os.path.isdir(output_folder):\n",
    "        print(\"请输入有效的文件夹路径\")\n",
    "        return\n",
    "\n",
    "    # 计数器\n",
    "    file_num = 0\n",
    "\n",
    "    # 打开Excel应用程序\n",
    "    app = xw.App(visible=False)\n",
    "\n",
    "    try:\n",
    "        # 起始时间\n",
    "        start_time = time.time()\n",
    "\n",
    "        # 遍历输入文件夹中的所有文件\n",
    "        for filename in os.listdir(input_folder):\n",
    "            # 获取文件的完整路径\n",
    "            input_path = os.path.join(input_folder, filename)\n",
    "\n",
    "            # 判断是否为.xls文件\n",
    "            if os.path.isfile(input_path) and os.path.splitext(filename)[1] == \".csv\":\n",
    "                # 计数器加1\n",
    "                file_num += 1\n",
    "\n",
    "                # 构建输出文件的完整路径\n",
    "                output_path = os.path.join(output_folder, filename.replace('.csv', '.xlsx'))\n",
    "\n",
    "                try:\n",
    "                    # 打开Excel文件\n",
    "                    wb = app.books.open(input_path)\n",
    "                    sheet = wb.sheets[0]\n",
    "\n",
    "                    # 另存为.xlsx格式并保留合并单元格\n",
    "                    wb.save(output_path)\n",
    "                except Exception as e:\n",
    "                    print(f\"转换文件 {input_path} 失败，错误信息：{str(e)}\")\n",
    "                finally:\n",
    "                    # 关闭Excel文件\n",
    "                    wb.close()\n",
    "\n",
    "                # 删除原始.xls文件\n",
    "                os.remove(input_path)\n",
    "\n",
    "        # 结束时间\n",
    "        end_time = time.time()\n",
    "        # 计算耗时\n",
    "        delta_time = end_time - start_time\n",
    "        print(f\"数据表格式转换耗时 {delta_time:.3f} 秒，共计转换 {file_num} 张表格\")\n",
    "\n",
    "    except Exception as e:\n",
    "        print(f\"转换过程中发生异常，错误信息：{str(e)}\")\n",
    "\n",
    "    finally:\n",
    "        # 关闭Excel应用程序\n",
    "        app.quit()\n",
    "\n",
    "# 指定包含.xls文件的文件夹\n",
    "input_folder = input('转换的文件路径：')\n",
    "#input_folder = 'D:\\新建文件夹 (2)'\n",
    "# 指定保存.xlsx文件的文件夹\n",
    "output_folder = input('保存的路径：')\n",
    "#output_folder = 'D:\\新建文件夹 (2)'\n",
    "# 执行转换操作\n",
    "convert_xls_to_xlsx(input_folder, output_folder)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
